package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Risk;
import com.itheima.service.RiskService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/risk")
public class RiskController {
    @Reference
    private RiskService riskService;

    //风险评估分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return riskService.findPage(queryPageBean);
    }

    //新增风险评估
    @RequestMapping("/add")
    public Result add(@RequestBody Risk risk) {
        try {
            riskService.add(risk);
            return new Result(true, MessageConstant.ADD_RISK_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_RISK_FAIL);
        }
    }

    //风险评估编辑表单回显
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Risk risk = riskService.findById(id);
            return new Result(true, MessageConstant.QUERY_RISK_SUCCESS, risk);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_RISK_FAIL);
        }
    }

    //风险评估编辑提交存储
    @RequestMapping("/edit")
    public Result edit(@RequestBody Risk risk) {
        try {
            riskService.edit(risk);
            return new Result(true, MessageConstant.UPDATE_RISK_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_RISK_FAIL);
        }
    }

    //风险评估删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            riskService.delete(id);
            return new Result(true, MessageConstant.DELETE_RISK_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_RISK_FAIL);
        }
    }
}
